<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
require_once APPPATH . 'controllers/application' . EXT;

class Dang_ky extends Application {

    public function __construct() {
        parent::__construct();
        session_start();
    }

    public function index() {
        $data = array();
        $this->titlePage = 'dang ky';
//        $this->load->helper("path");
//        $path = "adfad";
//        print_r(set_realpath($path));
        $dir = getcwd();
        print_r($dir);
        $this->breadcrumb->clear();
        $this->breadcrumb->add_crumb($this->home, '/dienhoa2/main');
        $this->breadcrumb->add_crumb("Form đăng ký");
        $data['quest'] = $this->user_model->getQuest();
        $captcha = $this->captcha_model->generateCaptcha();
        $_SESSION['captchaWord'] = $captcha['word'];
        $data['cap'] = $captcha['image'];
        
//        $pubkey = '6LfHVtYSAAAAAGGY7VafntTjBpTGw7WjJmz50bz5';
//        $data['html_captcha'] = recaptcha_get_html($pubkey);
        $this->render($this->load->view('register_view', $data, TRUE));
    }

    public function xl_dangky() {
        $config = array(
            array(
                'field' => 'acount',
                'label' => 'Tên đăng nhập',
                'rules' => 'trim|required|min_length[6]|xss_clean|is_unique[customer.CAcount]'
            ),
            array(
                'field' => 'password',
                'label' => 'Mật khẩu',
                'rules' => 'trim|required|xss_clean|min_length[6]'
            ),
            array(
                'field' => 'repassword',
                'label' => 'Nhập lại mật khẩu',
                'rules' => 'trim|xss_clean|required|matches[password]'
            ),
            array(
                'field' => 'fullName',
                'label' => 'Họ và tên',
                'rules' => 'required'
            ),
            array(
                'field' => 'address',
                'label' => 'Địa chỉ',
                'rules' => 'required'
            ),
            array(
                'field' => 'email',
                'label' => 'Email',
                'rules' => 'required|valid_email|is_unique[customer.CEmail]'
            ),
            array(
                'field' => 'quest',
                'label' => 'Câu hỏi bí mật',
                'rules' => 'required'
            ),
            array(
                'field' => 'answers',
                'label' => 'Câu trả lời',
                'rules' => 'required'
            ),
            array(
                'field' => 'captcha_input',
                'label' => 'Mã kiểm tra',
                'rules' => 'required|callback_CheckCapt'
            )
        );
        $this->form_validation->set_rules($config);
        if ($this->form_validation->run() == false) {
//            redirect('dang_ky', 'refresh');
//            echo validation_errors();
            $dk = array(
                'er_user' => form_error("acount"),
                'er_pass' => form_error("password"),
                'er_repass' => form_error("repassword"),
                'er_fullname' => form_error("fullName"),
                'er_addr' => form_error("address"),
                'er_email' => form_error("email"),
                'er_quest' => form_error("quest"),
                'er_answers' => form_error("answers"),
                'er_captcha' => form_error("captcha_input")
            );
            echo json_encode($dk);
            die;
        } else {
            $user = $this->input->post('acount');
            $pass = $this->input->post('password');
            $fullname = $this->input->post('fullName');
            $addr = $this->input->post('address');
            $email = $this->input->post('email');
            $phone = $this->input->post('phone');
            $gender = $this->input->post('gender');
            $quest = $this->input->post('quest');
            $cautraloi = $this->input->post('answers');
            if ($this->user_model->InsertUser($quest, $user, $pass, $fullname, $addr, $email, $phone, $gender, $cautraloi)) {
//                    $this->render($this->load->view('dk_thanh_cong', '', true));
                $dk = array(
                    'errors' => TRUE
                );
                echo json_encode($dk);
                die;
            }
        }
    }

    public function CheckCapt() {
        if (strcasecmp($_SESSION['captchaWord'], $_POST['captcha_input']) === 0) {
            return TRUE;
        } else {
            $this->form_validation->set_message('CheckCapt', '%s không đúng');
            return FALSE;
        }
    }

    function recaptcha_validation() {
        $return = recaptcha_check_answer('6LfHVtYSAAAAAOLSI2Wgqe4bekDr-uIiYqEUXehI', $_SERVER["REMOTE_ADDR"], $this->input->post("recaptcha_challenge_field"), $this->input->post("recaptcha_response_field"));

        if (!$return->is_valid) {
            $this->form_validation->set_message('recaptcha_validation', '%s không đúng');
            return FALSE;
        } else {
            return TRUE;
        }
    }

}

?>
